//
//  HDIncomeViewController.m
//  HDecoration
//
//  Created by 曹群 on 15/8/17.
//  Copyright (c) 2015年 HDecoration. All rights reserved.
//

#import "HDIncomeTableViewController.h"
#import "HDTakeMoneyViewController.h"

@interface HDIncomeTableViewController ()

@end

@implementation HDIncomeTableViewController

- (void)setIncomeArray:(NSArray *)incomeArray
{
    _incomeArray = [incomeArray copy];
    [self.tableView reloadData];
}

- (void)setTotalArray:(NSArray *)totalArray
{
    _totalArray = [totalArray copy];
    [self filterIncomeList:[NSDate date]];
}

- (void)filterIncomeList:(NSDate *)date
{
    NSString *dateStr = [HDUtil formatter:@"yyyy-MM" FromeDate:date];
    NSMutableArray *totalFilterIncomeArray = [[NSMutableArray alloc] initWithCapacity:0];
    for (HDIncomeInfo *incomeInfo in self.totalArray) {
        if ([incomeInfo.date isEqualToString:dateStr]) {
            [totalFilterIncomeArray addObject:incomeInfo];
        }
    }
    NSMutableArray *filterIncomeArray = [[NSMutableArray alloc] initWithCapacity:0];
    NSString *tmpDate = ((HDIncomeInfo *)[totalFilterIncomeArray firstObject]).date;
    NSMutableArray *tmpIncomeArray = [[NSMutableArray alloc] initWithCapacity:0];
    for (int i=0;i<totalFilterIncomeArray.count;i++) {
        HDIncomeInfo *incomeInfo = totalFilterIncomeArray[i];
        if (incomeInfo.date==tmpDate) {
            [tmpIncomeArray addObject:incomeInfo];
        } else {
            [filterIncomeArray addObject:[tmpIncomeArray copy]];
            [tmpIncomeArray removeAllObjects];
            [tmpIncomeArray addObject:incomeInfo];
        }
        tmpDate = incomeInfo.date;
        if (i==totalFilterIncomeArray.count-1) {
            [filterIncomeArray addObject:[tmpIncomeArray copy]];
        }
    }
    self.incomeArray = filterIncomeArray;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.incomeArray.count+1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section==0) {
        return 1;
    } else {
        return ((NSArray *)self.incomeArray[section-1]).count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identify = indexPath.section==0?@"incomCell":@"incomeInfoCell";
    if (indexPath.section==0)
    {
        HDIncomeCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
        if (cell==nil) {
            cell = [[HDIncomeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.delegate = self;
        }
        cell.income = self.income;
        return cell;
    } else {
        HDIncomeInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
        if (cell==nil) {
            cell = [[HDIncomeInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.incomeInfo = ((NSArray *)self.incomeArray[indexPath.section-1])[indexPath.row];
        return cell;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section!=0&&self.incomeArray.count) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, APPFRAME.size.width, 10)];
        view.backgroundColor = [UIColor whiteColor];
        UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, APPFRAME.size.width, 0.5)];
        line.image = [UIImage imageNamed:@"dot"];
        [view addSubview:line];
        
        UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, APPFRAME.size.width-20, 40)];
        dateLabel.font = [UIFont systemFontOfSize:15];
        dateLabel.textColor = [UIColor hexStringToColor:@"#868686"];
        HDIncomeInfo *incomInfo = [self.incomeArray[section-1] firstObject];
        dateLabel.text = incomInfo.week;
        [view addSubview:dateLabel];
        
        UIImageView *bottomLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, dateLabel.bottom-0.5, APPFRAME.size.width, 0.5)];
        bottomLine.image = [UIImage imageNamed:@"dot"];
        [view addSubview:bottomLine];
        return view;
    } else {
        return nil;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section!=0&&self.incomeArray.count) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, APPFRAME.size.width, 10)];
        view.backgroundColor = [UIColor whiteColor];
        return view;
    } else {
        return nil;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        return [HDIncomeCell cellHeight];
    } else {
        return 40;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section!=0&&self.incomeArray.count) {
        return 50;
    }
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section!=0&&self.incomeArray.count) {
        return 10;
    }
    return 0.01;
}

@end

@interface HDIncomeCell ()
@property(nonatomic,strong)UIImageView *backgroundImage;
@property(nonatomic,strong)UILabel *yearLabel;
@property(nonatomic,strong)GXButton *timeSelectBtn;
@property(nonatomic,strong)UILabel *incomeLabel;
@property(nonatomic,strong)UIButton *takeMoneyBtn;
@property(nonatomic,strong)UILabel *dateLabel;
@property(nonatomic,strong)HDDatePickerView *datePickerView;
@end

@implementation HDIncomeCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.contentView addSubview:self.backgroundImage];
        [self.backgroundImage addSubview:self.yearLabel];
        [self.backgroundImage addSubview:self.timeSelectBtn];
        [self.backgroundImage addSubview:self.incomeLabel];
        [self.backgroundImage addSubview:self.takeMoneyBtn];
        [self setDate:[NSDate date]];
    }
    return self;
}

- (void)setDate:(NSDate *)date
{
    self.yearLabel.text = [HDUtil formatter:@"yyyy年" FromeDate:date];
    [self.timeSelectBtn setTitle:[HDUtil formatter:@"MM月" FromeDate:date] forState:UIControlStateNormal];
    if ([self.delegate respondsToSelector:@selector(filterIncomeList:)]) {
        [self.delegate performSelector:@selector(filterIncomeList:) withObject:date];
    }
}

- (void)selectTime:(UIButton *)sender
{
    [self.datePickerView show];
}

- (void)takeMoney:(UIButton *)sender
{
    HDTakeMoneyViewController *takeMoneyVC = [[HDTakeMoneyViewController alloc] init];
    takeMoneyVC.navigationItem.title = @"余额提现";
    [rootNavigation pushViewController:takeMoneyVC animated:YES];
}

- (void)setIncome:(float)income
{
    _income = income;
    NSMutableAttributedString *incomeStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"当前余额：%d元",(int)_income]];
    NSRange range = {5,incomeStr.length-5};
    [incomeStr addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:range];
    self.incomeLabel.attributedText = incomeStr;
}

- (void)datePickerPickDone:(NSDate *)date
{
    [self setDate:date];
}

+(CGFloat)cellHeight
{
    return (APPFRAME.size.width-20)*138/600.0+10;
}

- (UIImageView *)backgroundImage
{
    if (!_backgroundImage) {
        _backgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, APPFRAME.size.width-20, (APPFRAME.size.width-20)*138/600.0)];
        _backgroundImage.image = [UIImage imageNamed:@"收入背景图"];
        _backgroundImage.userInteractionEnabled = YES;
    }
    return _backgroundImage;
}

- (UILabel *)yearLabel
{
    if (!_yearLabel) {
        _yearLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, self.backgroundImage.width/4.0, 20)];
        _yearLabel.font = [UIFont systemFontOfSize:15];
        _yearLabel.textColor = [UIColor whiteColor];
        _yearLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _yearLabel;
}

- (GXButton *)timeSelectBtn
{
    if (!_timeSelectBtn) {
        _timeSelectBtn = [GXButton buttonWithType:UIButtonTypeCustom];
        _timeSelectBtn.frame = CGRectMake(10, self.yearLabel.bottom, self.yearLabel.width-10, 30);
        [_timeSelectBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _timeSelectBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        _timeSelectBtn.titleLabelFrame = CGRectMake(0, 7, 35, 15);
        _timeSelectBtn.imageViewFrame = CGRectMake(40, 7, 15, 15);
        _timeSelectBtn.titleLabel.textAlignment = NSTextAlignmentRight;
        [_timeSelectBtn setImage:[UIImage imageNamed:@"下拉箭头"] forState:UIControlStateNormal];
        _timeSelectBtn.relayout = YES;
        [_timeSelectBtn addTarget:self action:@selector(selectTime:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _timeSelectBtn;
}

- (UILabel *)incomeLabel
{
    if (!_incomeLabel) {
        _incomeLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.backgroundImage.width/4.0+10, 0, self.backgroundImage.width/2.0, self.backgroundImage.height)];
        _incomeLabel.textColor = [UIColor whiteColor];
        _incomeLabel.font = [UIFont systemFontOfSize:15];
    }
    return _incomeLabel;
}

- (UIButton *)takeMoneyBtn
{
    if (!_takeMoneyBtn) {
        _takeMoneyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _takeMoneyBtn.frame = CGRectMake(self.backgroundImage.width-60, self.backgroundImage.height/2.0-25, 50, 50);
        [_takeMoneyBtn setBackgroundImage:[UIImage imageNamed:@"取现按钮"] forState:UIControlStateNormal];
        [_takeMoneyBtn setTitleColor:[UIColor hexStringToColor:@"#e94929"] forState:UIControlStateNormal];
        [_takeMoneyBtn setTitle:@"取现" forState:UIControlStateNormal];
        _takeMoneyBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_takeMoneyBtn addTarget:self action:@selector(takeMoney:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _takeMoneyBtn;
}

- (HDDatePickerView *)datePickerView
{
    if (!_datePickerView) {
        _datePickerView = [[HDDatePickerView alloc] initWithFrame:CGRectMake(0, SCREENFRAME.size.height-200, APPFRAME.size.width, 200)];
        _datePickerView.delegate = (id)self;
    }
    return _datePickerView;
}

@end

@interface HDIncomeInfoCell ()
@property(nonatomic,strong)UIButton *typeBtn;
@property(nonatomic,strong)UILabel *detailInfoLabel;
@property(nonatomic,strong)UILabel *numberLabel;
@end

@implementation HDIncomeInfoCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.contentView addSubview:self.typeBtn];
        [self.contentView addSubview:self.detailInfoLabel];
        [self.contentView addSubview:self.numberLabel];
    }
    return self;
}

- (void)setIncomeInfo:(HDIncomeInfo *)incomeInfo
{
    _incomeInfo = [incomeInfo copy];
    
    if([incomeInfo.type rangeOfString:@"收"].location!=NSNotFound)
    {
        [self.typeBtn setBackgroundImage:[UIImage imageNamed:@"收"] forState:UIControlStateNormal];
        [self.typeBtn setTitle:@"收" forState:UIControlStateNormal];
    }
    else
    {
        [self.typeBtn setBackgroundImage:[UIImage imageNamed:@"支"] forState:UIControlStateNormal];
        [self.typeBtn setTitle:@"支" forState:UIControlStateNormal];
    }
    self.detailInfoLabel.text = _incomeInfo.detailInfo;
    self.numberLabel.text = _incomeInfo.number;
}

- (UIButton *)typeBtn
{
    if (!_typeBtn) {
        _typeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _typeBtn.frame = CGRectMake(10, 10, 25, 25);
        _typeBtn.layer.cornerRadius = 12.5;
        _typeBtn.layer.masksToBounds = YES;
        [_typeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _typeBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        _typeBtn.userInteractionEnabled = NO;
    }
    return _typeBtn;
}

- (UILabel *)detailInfoLabel
{
    if (!_detailInfoLabel) {
        _detailInfoLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.typeBtn.right+10, 10, APPFRAME.size.width-140, 25)];
        _detailInfoLabel.font = [UIFont systemFontOfSize:14];
        _detailInfoLabel.textColor = kDarkGrayColor;
    }
    return _detailInfoLabel;
}

- (UILabel *)numberLabel
{
    if (!_numberLabel) {
        _numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(APPFRAME.size.width-80, 10, 60, 25)];
        _numberLabel.font = [UIFont systemFontOfSize:14];
        _numberLabel.textColor = kDarkGrayColor;
    }
    return _numberLabel;
}

@end
